package com.cloudkinto.utils;

import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.entity.OrderRakutenDo;
import com.cloudkinto.entity.OrderRakutenPackageDo;
import com.cloudkinto.entity.OrderRakutenProductDo;
import com.cloudkinto.service.order.vo.rakuten.ShippingModelEntity;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1.创建配置实例
 * 2.获得模板
 * 3.创建数据模型[数据模型可以是List、Map对象 注意:Map类型的key必须是String类型]
 * 4.将模板和数据模型合并
 *
 * @author 郑清
 */
public class FreeMarkerUtils {


    public static String createRakutentConfirmMailTemplate(OrderRakutenDo orderModel, OrderRakutenPackageDo orderRakutenPackageDo,
                                                           List<OrderRakutenProductDo> orderRakutenProductList, String path) throws Exception {
        StringWriter stringWriter = new StringWriter();
        Template template = getTemplate(path);//hello.ftl是模板名称
        Map map = new HashMap();
        map.put("orderModel", orderModel);
        map.put("packageModel", orderRakutenPackageDo);
        map.put("productModelList", orderRakutenProductList);
        final int[] totalCount = new int[1];
        orderRakutenProductList.stream().forEach(item -> {
            totalCount[0] = totalCount[0] + item.getUnits();
        });
        map.put("productCount", orderRakutenProductList.size());
        map.put("totalCount", totalCount[0]);
        template.process(map, stringWriter);
        stringWriter.flush();
        return stringWriter.toString();
    }

    private static Template getTemplate(String s) throws Exception {
        StringWriter stringWriter = new StringWriter();
        //①创建配置对象
        Configuration cfg = new Configuration();
        File f = new File(s);
        System.out.println(f.getParent());
        //②读取模板文件夹
        cfg.setDirectoryForTemplateLoading(f.getParentFile());//设置要加载的模板文件的路径
        //③设置模板的编码格式
        cfg.setDefaultEncoding("UTF-8");
        //④获取模板对象
        return cfg.getTemplate(f.getName());//xxx.ftl是模板名称
    }

    public static String createRakutenSendOutMailTemplate(OrderRakutenDo orderModel, OrderRakutenPackageDo orderRakutenPackageDo,
                                                          List<OrderRakutenProductDo> orderRakutenProductList,
                                                          ShippingModelEntity shippingModelEntity, String path) throws Exception {
        StringWriter stringWriter = new StringWriter();
        //④获取模板对象
        Template template = getTemplate(path);//hello.ftl是模板名称
        Map map = new HashMap();
        map.put("orderModel", orderModel);
        map.put("packageModel", orderRakutenPackageDo);
        map.put("productModelList", orderRakutenProductList);

        map.put("request", shippingModelEntity);
        final int[] totalCount = new int[1];
        orderRakutenProductList.stream().forEach(item -> {
            totalCount[0] = totalCount[0] + item.getUnits();
        });
        map.put("productCount", orderRakutenProductList.size());
        map.put("totalCount", totalCount[0]);
        template.process(map, stringWriter);
        stringWriter.flush();
        return stringWriter.toString();
    }

    /**
     * 导出word
     *
     * @param response
     * @param templateName
     * @param dataMap
     */
    public static void downLoadWordTemp(HttpServletResponse response, String path, String templateName, Map<String, Object> dataMap) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/msword");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(templateName, "UTF-8"));

        OutputStream os = response.getOutputStream();
        Writer writer = new OutputStreamWriter(os, "utf-8");
        Template template = null;
        try {
            template = getTemplate(path);
            template.process(dataMap, writer);
            os.flush();
            writer.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String showTemplate(String path, List item) {
        //新建一个写的流
        StringWriter writer = new StringWriter();
        Map map=new HashMap();
        map.put("item",item);
        try {
            //创建模板类
            Template template = getTemplate(path);
            template.process(map, writer);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writer.toString();
    }
}